<template>
  <div class="signin-container">
    <!-- 操作栏 -->
    <div class="action-bar">
      <el-input
        v-model="searchKey"
        placeholder="搜索签到标题"
        style="width: 300px; margin-left: 20px"
        clearable
        @input="handleSearchInput"
      >
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
      <el-select 
        style="width:180px; margin-left: 20px" 
        v-model="signType" 
        @change="fetchData" 
        placeholder="签到类型"
      >
        <el-option label="全部" value="" />
        <el-option label="普通签到" value="普通" />
        <el-option label="签到码签到" value="签到码" />
        <el-option label="二维码签到" value="二维码" />
        <el-option label="位置签到" value="位置" />
        <el-option label="手势签到" value="手势" />
      </el-select>
    </div>

    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      style="width: 100%; margin-top: 20px"
      empty-text="暂无签到数据"
    >
      <el-table-column prop="title" label="签到标题" width="250" />
      <el-table-column prop="signedCount" label="参与人数" width="120" />
      <el-table-column prop="range" label="迟到范围" width="200">
        <template #default="scope">
          {{ scope.row.range }} 分钟
        </template>
      </el-table-column>
      <el-table-column prop="classes" label="参与班级" width="120" />
      <el-table-column prop="signType" label="签到类型" width="120">
        <template #default="scope">
          {{ scope.row.signType}}签到
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button size="small" type="primary" @click="handleDetail(row)">详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50]"
        :total="total"
        layout="total, sizes, prev, pager, next"
        @size-change="fetchData"
        @current-change="fetchData"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import axios from 'axios'
// import dayjs from 'dayjs'
import { debounce } from 'lodash-es';
import { useRouter } from 'vue-router'
const router = useRouter()

const API_BASE = 'http://localhost:8080/attendance'
const api = {
  list: `${API_BASE}/listResult`,
}

// 状态管理
const tableData = ref([])
const loading = ref(false)
const searchKey = ref('')
const signType = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

onMounted(() => {
  fetchData()
})

const handleSearchInput = debounce(() => {
  fetchData();
}, 500);

const fetchData = async () => {
  try {
    loading.value = true
    const params = {
      currentPage: currentPage.value,
      pageSize: pageSize.value,
      title: searchKey.value,
      sessionType: signType.value
    }
    const response = await axios.get(api.list, { params })
    const pageData = response.data.data
    tableData.value = pageData.records.map(item => ({
      id: item.sessionId,
      title: item.title,
      range: item.lateRange,
      classes: item.classId,
      signType: item.sessionType,
      signedCount: item.number // 新增已签到人数字段
    }))
    total.value = pageData.total
    currentPage.value = pageData.current
    pageSize.value = pageData.size
  } catch (error) {
    ElMessage.error('数据加载失败')
  } finally {
    loading.value = false
  }
}

const handleDetail = (row) => {
  router.push(`/record/${row.id}`); // 假设需要传递签到ID到详情页
}
</script>

<style scoped>
.signin-container {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
}

.action-bar {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>